/**
 * SEO相关工具函数
 */

interface SEOConfig {
  title?: string;
  description?: string;
  keywords?: string;
  ogTitle?: string;
  ogDescription?: string;
  ogImage?: string;
  canonicalUrl?: string;
}

const defaultSEO = {
  title: "数知笔记 - 专业技术博客",
  description:
    "专注于技术分享、编程教程、数据分析和知识管理的专业博客平台。提供高质量的原创技术文章和实用教程。",
  keywords: "技术博客,编程教程,数据分析,Vue.js,JavaScript,前端开发,后端开发,软件开发",
  ogImage: "/default-og-image.jpg",
};

/**
 * 设置页面SEO信息
 * @param config SEO配置
 */
export function setSEO(config: SEOConfig) {
  // 设置页面标题
  if (config.title) {
    document.title = `${config.title} - 数知笔记`;
    // 更新og:title
    updateMetaTag("property", "og:title", config.ogTitle || config.title);
    updateMetaTag("property", "twitter:title", config.ogTitle || config.title);
  }

  // 设置描述
  if (config.description) {
    updateMetaTag("name", "description", config.description);
    updateMetaTag("property", "og:description", config.ogDescription || config.description);
    updateMetaTag("property", "twitter:description", config.ogDescription || config.description);
  }

  // 设置关键词
  if (config.keywords) {
    updateMetaTag("name", "keywords", config.keywords);
  }

  // 设置图片
  if (config.ogImage) {
    updateMetaTag("property", "og:image", config.ogImage);
    updateMetaTag("property", "twitter:image", config.ogImage);
  }

  // 设置canonical URL
  if (config.canonicalUrl) {
    updateCanonicalUrl(config.canonicalUrl);
  }
}

/**
 * 更新或创建meta标签
 */
function updateMetaTag(attr: "name" | "property", value: string, content: string) {
  let element = document.querySelector(`meta[${attr}="${value}"]`) as HTMLMetaElement;

  if (!element) {
    element = document.createElement("meta");
    element.setAttribute(attr, value);
    document.head.appendChild(element);
  }

  element.setAttribute("content", content);
}

/**
 * 更新canonical URL
 */
function updateCanonicalUrl(url: string) {
  let element = document.querySelector("link[rel=\"canonical\"]") as HTMLLinkElement;

  if (!element) {
    element = document.createElement("link");
    element.setAttribute("rel", "canonical");
    document.head.appendChild(element);
  }

  element.setAttribute("href", url);
}

/**
 * 设置默认SEO
 */
export function setDefaultSEO() {
  setSEO(defaultSEO);
}

/**
 * 生成结构化数据
 */
export function setStructuredData(data: any) {
  // 移除现有的结构化数据
  const existing = document.querySelector("script[type=\"application/ld+json\"]");
  if (existing) {
    existing.remove();
  }

  // 添加新的结构化数据
  const script = document.createElement("script");
  script.type = "application/ld+json";
  script.textContent = JSON.stringify(data);
  document.head.appendChild(script);
}

/**
 * 为文章页面设置结构化数据
 */
export function setArticleStructuredData(article: {
  title: string;
  description: string;
  author: string;
  publishDate: string;
  modifiedDate?: string;
  image?: string;
  url: string;
}) {
  const structuredData = {
    "@context": "https://schema.org",
    "@type": "Article",
    "headline": article.title,
    "description": article.description,
    "author": {
      "@type": "Person",
      "name": article.author,
    },
    "datePublished": article.publishDate,
    "dateModified": article.modifiedDate || article.publishDate,
    "image": article.image,
    "url": article.url,
    "publisher": {
      "@type": "Organization",
      "name": "数知笔记",
      "logo": {
        "@type": "ImageObject",
        "url": "/logo.png",
      },
    },
  };

  setStructuredData(structuredData);
}

/**
 * 为网站设置组织结构化数据
 */
export function setOrganizationStructuredData() {
  const structuredData = {
    "@context": "https://schema.org",
    "@type": "Organization",
    "name": "数知笔记",
    "description": "专注于技术分享、编程教程、数据分析和知识管理的专业博客平台",
    "url": window.location.origin,
    "logo": window.location.origin + "/logo.png",
    "sameAs": [
      // 这里可以添加社交媒体链接
    ],
  };

  setStructuredData(structuredData);
} 